package com.zzhardy.common.utils.code;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import org.beetl.sql.core.*;
import org.beetl.sql.core.db.DBStyle;
import org.beetl.sql.core.db.MySqlStyle;
import org.beetl.sql.core.db.TableDesc;
import org.beetl.sql.core.kit.GenKit;
import org.beetl.sql.core.kit.StringKit;
import org.beetl.sql.ext.DebugInterceptor;
import org.beetl.sql.ext.gen.GenConfig;
import org.beetl.sql.ext.gen.MDCodeGen;
import org.beetl.sql.ext.gen.MapperCodeGen;
import org.junit.Test;

public class GenCode {
    // ========模板的路径, 示例是spring boot的[src/main/resources/beetlsqlTemplate 文件夹]=========
    private static String templatePath = "/sql/template";
    // ========权限md生成路径 要提前创建=========
    private static String permissionMdPath = "/sql/admin/permission";
    
    /**
     *  获取SQLManager对象
     * @return
     */
    public static SQLManager getSqlManager() {
        String           driver   = "com.mysql.cj.jdbc.Driver";
/*        String           url      = "jdbc:mysql://127.0.0.1:3306/growth_blog?useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2B8&useSSL=false&useInformationSchema=true";
        String           userName = "root";
        String           password = "123456";*/
        String           url      = "jdbc:mysql://47.94.246.193:3306/growth_blog?useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2B8&useSSL=false&useInformationSchema=true";
        String           userName = "adore";
        String           password = "hardy1647292584";
        ConnectionSource source   = ConnectionSourceHelper.getSimple(driver, url, userName, password);
        DBStyle          mysql    = new MySqlStyle();
        // sql语句放在classpagth的/sql 目录下
        SQLLoader loader = new ClasspathLoader("/sql");
        // 数据库命名跟java命名一样，所以采用DefaultNameConversion，还有一个是UnderlinedNameConversion，下划线风格的，
        UnderlinedNameConversion nc = new UnderlinedNameConversion();
        // 最后，创建一个SQLManager,DebugInterceptor 不是必须的，但可以通过它查看sql执行情况
        SQLManager sqlManager = new SQLManager(mysql, loader, source, nc, new Interceptor[]{new DebugInterceptor()});

        return sqlManager;
    }
    /**
     * 生成md文件
     */
    public static void genMd(SQLManager sqlManager, GenConfig config, String table, String permissionMdPath) throws IOException {
        String fileName = StringKit.toLowerCaseFirstOne(sqlManager.getNc().getClassName(table));
        if (config.getIgnorePrefix() != null && !config.getIgnorePrefix().trim().equals("")) {
            fileName = fileName.replaceFirst(StringKit.toLowerCaseFirstOne(config.getIgnorePrefix()), "");
            fileName = StringKit.toLowerCaseFirstOne(fileName);
        }
        String     target    = GenKit.getJavaResourcePath() + "/" + permissionMdPath + "/" + fileName + ".md";
        TableDesc  desc      = sqlManager.getMetaDataManager().getTable(table);
        FileWriter writer    = new FileWriter(new File(target));
        MDCodeGen  mdCodeGen = new MDCodeGen();
        mdCodeGen.setMapperTemplate(config.getTemplate(templatePath + "/md.btl"));
        mdCodeGen.genCode(sqlManager.getBeetl(), desc, sqlManager.getNc(), null, writer);
        writer.close();
    }

    
    /**
     *  生成权限系统基本的del基础系统的表Model, 业务完成后禁止再次执行
     * @throws Exception
     */
    @Test
    public void permissionDemo() throws Exception {
        SQLManager sqlManager = getSqlManager();
        GenConfig  config     = new GenConfig();
        config.preferBigDecimal(true);
        // 生成mapper
        //MapperCodeGen mapper = new MapperCodeGen("com.zzhardy.blog.web.admin.permission.dao");
        //config.codeGens.add(mapper);
        // 生成model
        sqlManager.genPojoCode("sys_dict","com.zzhardy.controller.admin.permission.model",config);
        sqlManager.genPojoCode("sys_menu","com.zzhardy.controller.admin.permission.model",config);
        sqlManager.genPojoCode("sys_role_user","com.zzhardy.controller.admin.permission.model",config);
        sqlManager.genPojoCode("sys_user","com.zzhardy.controller.admin.permission.model",config);
        sqlManager.genPojoCode("sys_role","com.zzhardy.controller.admin.permission.model",config);
        // 生成md
        //genMd(sqlManager, config, "sys_system", permissionMdPath);
        //genMd(sqlManager, config, "sys_unit", permissionMdPath);
        //genMd(sqlManager, config, "sys_dict", permissionMdPath);
        //genMd(sqlManager, config, "sys_menu", permissionMdPath);
        //genMd(sqlManager, config, "sys_role_user", permissionMdPath);
        //genMd(sqlManager, config, "sys_user", permissionMdPath);
        //genMd(sqlManager, config, "sys_role", permissionMdPath);
    }

    /**
     *  生成blog系统的model
     * @throws Exception
     */
    @Test
    public void blogDemo() throws Exception {
        SQLManager sqlManager = getSqlManager();
        GenConfig  config     = new GenConfig();
        config.preferBigDecimal(true);
        // 生成mapper
        MapperCodeGen mapper = new MapperCodeGen("com.zzhardy.modules.blog.dao");
        config.codeGens.add(mapper);
        // 生成model
        sqlManager.genPojoCode("b_blog","com.zzhardy.modules.blog.model",config);
        sqlManager.genPojoCode("b_message","com.zzhardy.modules.blog.model",config);
    }

}
